<?php
namespace Home\Controller;
use Think\Controller;

class CommodityController extends CommonController {
	
	public function  _initialize(){
		parent::_initialize();
		$this->cartSrv = D('CartTmp');
		$this->commoditySrv = D('CommodityInfo');
		$this->commoditySrv->ini();
    }
	
	public function ajaxGetCommodityInfo() {
		$commodity_id = I('commodity_id');
		$base_info = $this->commoditySrv->commodity_info($commodity_id);
		if(empty($base_info)){
			$response['msg'] = '操作失败';
			$response['rsc'] = C('RESPONSE.FAILED');
			$this->ajaxReturn($response);
		}
		$base_info['unit_name'] = C('GOODS_UNIT_NAME.'.$base_info['unit_id']);
		$commodityLimit = D('CommodityLimit');
		$limit_res = $commodityLimit->getLimit($commodity_id);
		$base_info['is_limit'] = $limit_res['is_limit'];
		$base_info['left_num'] = $limit_res['left_num'];
		$ext_imgs = array();
		$commodityPicInfo = D('CommodityPicInfo');
		$db_res = $commodityPicInfo->where("commodity_id = '".$commodity_id."'")->select();
		foreach($db_res as $db_one){
			$tmp['brief_pic'] = C('HTTP_IMG').$db_one['brief_pic'];
			$tmp['type'] = $db_one['type'];
			$ext_imgs[] = $tmp;
		}
		//购物车信息
		if($this->checkLogin()==true){
			$result = $this->cartSrv->getCartList($this->uId());
			$cart_list = $result['data'];
		}
		//搭配信息
		$commodityLimit = D('CommodityLimit');
		$limitArr = $commodityLimit->getLimitArr();
		$collocations = array();
		$collocation = D('CommodityCollocation');
		$db_res = $collocation->where("commodity_id = '".$commodity_id."'")->find();
		if(!empty($db_res['collocations'])){
			$tmparr = explode(",",$db_res['collocations']);
			foreach($tmparr as $tmpid){
				$tmp_commodity = $this->commoditySrv->commodity_info($tmpid);
				$tmp_commodity['unit_name'] = C('GOODS_UNIT_NAME.'.$tmp_commodity['unit_id']); 
				if(isset($limitArr[$tmpid])){
					$tmp_commodity['left_num'] = num_f($limitArr[$tmpid]);
					$tmp_commodity['is_limit'] = true;
				}else{
					$tmp_commodity['left_num'] = 0;
					$tmp_commodity['is_limit'] = false;
				}
				$collocations[] = $tmp_commodity;
			}
		}
		$tmp = $this->commoditySrv->where("commodity_id = '".$commodity_id."'")->find();
		$base_info['desc'] = $tmp['desc'];
		
		//视频信息
		$commodityVideoInfo = D('CommodityVideoInfo');
		$videoInfo = $commodityVideoInfo->where("commodity_id = '".$commodity_id."'")->find();
		$base_info['video_id'] = $videoInfo['video_id'];
		//商品须知 
		$has_rule = 0;
		$blockManage = D('BlockManage');
		$db_res = $blockManage->where("block_id = 5 and type = 5 and is_active = 1")->find();
		if(!empty($db_res) && !empty($db_res['context'])){
			$context_arr = explode(",",$db_res['context']);
			if(in_array($commodity_id,$context_arr)){
				$has_rule = 1;
			}
		}
		$base_info['has_rule'] = $has_rule;
		$response['data']['base_info'] = $base_info;
		$response['data']['last_sale_cycle'] = $this->commoditySrv->hitLastSaleCycle($commodity_id);
		$response['data']['ext_imgs'] = $ext_imgs;
		$response['data']['cart_list'] = $cart_list;
		$response['data']['collocations'] = $collocations;
		$response['msg'] = '操作成功';
		$response['rsc'] = C('RESPONSE.SUCCESS');
		$this->ajaxReturn($response);
	}
	
	// 1：蔬鲜 2：果鲜 3：海鲜 4：肉鲜 5：八鲜 
	public function ajaxGetCommodityListByCateId(){
		$cate_id = I('cate_id');
		$commodityCategory = D('CommodityCategory');
		$db_res = $commodityCategory->where("category_id = '".$cate_id."'")->select();
		if(empty($db_res)){
			$response['data'] = null;
			$response['msg'] = '操作成功';
			$response['rsc'] = C('RESPONSE.SUCCESS');
			$this->ajaxReturn($response);
		}
		foreach($db_res as $db_one){
			$context_arr[] = $db_one['commodity_id'];
		}
		$commodityLimit = D('CommodityLimit');
		$limitArr = $commodityLimit->getLimitArr();
		$commodity_tmp = $this->commoditySrv->commodity_info();
		foreach($commodity_tmp as $commodity){
			if($commodity['is_onsale'] && in_array($commodity['commodity_id'],$context_arr)){
				$commodity['unit_name'] = C('GOODS_UNIT_NAME.'.$commodity['unit_id']);
				if(isset($limitArr[$commodity['commodity_id']])){
					$commodity['left_num'] = num_f($limitArr[$commodity['commodity_id']]);
					$commodity['is_limit'] = true;
				}else{
					$commodity['left_num'] = 0;
					$commodity['is_limit'] = false;
				}
				$commodity_list[] = $commodity;
			}
		}
		$response['data']['commodity_list'] = $commodity_list;
		$response['msg'] = '操作成功';
		$response['rsc'] = C('RESPONSE.SUCCESS');
		$this->ajaxReturn($response);
	}
	

};